package com.frame.pageform.tag;

import java.io.IOException;
import java.lang.reflect.Method;

import org.apache.commons.lang.StringUtils;

import com.frame.metadata.wordbook.WordbookService;
import com.frame.pageform.ResponseUtils;
import com.frame.util.DataConvert;
import com.frame.util.EncryptUtil;
import com.frame.util.SelectItem;

public class WriteTag extends BaseTag{

	private String bind;
	private String format;
	private String enumClass;
	private String enumKey;
	private String enumValue;
	private Integer length;
	private String dict;
	private String defaultVal;
	private boolean encrypt;//加密输出

	public int start() throws Exception
	{

		try {
			Object value = executeExpression(bind.replaceAll("'", "\""));
			if(getEnumClass()!=null && !"".equals(getEnumClass())){
				if(getEnumKey()==null || "".equals(getEnumKey()))
					throw new Exception("没有定义enumKey");
				else if(getEnumValue()==null || "".equals(getEnumValue()))
					throw new Exception("没有定义enumValue");
				if(value!=null){
					Class c=Class.forName(getEnumClass());
					Enum[] enums=(Enum[])c.getEnumConstants();
					for(Enum enumb:enums){
						String methodName = Character.toUpperCase(enumKey.charAt(0)) +enumKey.substring(1);
						Method getMethod = enumb.getClass().getMethod("get"+methodName,new Class[0]);
						Object obj=getMethod.invoke(enumb,new Object[0]);
						if(obj.toString().equals(value.toString())){
							String methodNamev = Character.toUpperCase(enumValue.charAt(0)) +enumValue.substring(1);
							Method getMethodv = enumb.getClass().getMethod("get"+methodNamev,new Class[0]);
							value=getMethodv.invoke(enumb,new Object[0]);
						}

					}
				}
			}
			if(StringUtils.isNotBlank(dict) && value!=null){
				SelectItem item=WordbookService.getIntance().getItemName(dict, value.toString());
				if(item!=null)
					value=item.getText();
			}
			String s = DataConvert.toString(value,format);
			if(format==null || "".equals(format))
				s=ResponseUtils.filter(s);
			if(StringUtils.isBlank(s) && StringUtils.isNotBlank(defaultVal))
				s=defaultVal;
			if(length!=null && s!=null){
				if(s.length()>length)
					s=s.substring(0,length)+"...";
			}
			if(isEncrypt())s=EncryptUtil.desEncode(s);
			write(s);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		return 0;
	}

	public String getBind() {
		return bind;
	}

	public void setBind(String bind) {
		this.bind = bind;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getEnumClass() {
		return enumClass;
	}

	public void setEnumClass(String enumClass) {
		this.enumClass = enumClass;
	}

	public String getEnumKey() {
		return enumKey;
	}

	public void setEnumKey(String enumKey) {
		this.enumKey = enumKey;
	}

	public String getEnumValue() {
		return enumValue;
	}

	public void setEnumValue(String enumValue) {
		this.enumValue = enumValue;
	}

	public Integer getLength() {
		return length;
	}

	public void setLength(Integer length) {
		this.length = length;
	}

	public String getDict() {
		return dict;
	}

	public void setDict(String dict) {
		this.dict = dict;
	}

	public String getDefaultVal() {
		return defaultVal;
	}

	public void setDefaultVal(String defaultVal) {
		this.defaultVal = defaultVal;
	}

	public boolean isEncrypt() {
		return encrypt;
	}

	public void setEncrypt(boolean encrypt) {
		this.encrypt = encrypt;
	}

}
